package com.example.smartcity.ui.dashboard;

import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.PatientsCardAdapter;
import com.example.smartcity.ui.Bean.patientsCardItemBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class addPersionCard extends AppCompatActivity {

    private ListView LV_userOrder;
    Gson gson = null;
    //卡片高度
    private int height = 500;
    //卡片个数
    private int index = 0;
    private CardView addPatient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_persion_card);
        LV_userOrder = findViewById(R.id.LV_userOrder);
        addPatient = findViewById(R.id.addPatient);
        initView();
        addPatient();
        reservation();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        String userId = getSharedPreferences("selfMessage",MODE_PRIVATE).getString("userID",null);
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/userinfo/patient/list?pageNum=1&pageSize=10&userId="+ userId;
        OkHttpUtils.getCall(newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG:", "onFailure: "+e.getMessage());
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                gson = new Gson();
                String Json = response.body().string();
                patientsCardItemBean bean = gson.fromJson(Json,patientsCardItemBean.class);
                if (bean.getCode()==200){
                    List<patientsCardItemBean.RowsBean> list = bean.getRows();
                    index = bean.getTotal();
                    LV_userOrder.post(new Runnable() {
                        @Override
                        public void run() {
                            ViewGroup.LayoutParams layoutParams = LV_userOrder.getLayoutParams();
                            layoutParams.height =index * height;
                            LV_userOrder.setLayoutParams(layoutParams);
                            LV_userOrder.setAdapter(new PatientsCardAdapter(getBaseContext(),R.layout.userorderitemlayout,list));
                        }
                    });

                }
            }
        });
    }
    //添加就诊人
    public void addPatient(){
        addPatient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(addPersionCard.this.getBaseContext(),addPaient.class));
            }
        });

    }
    public void reservation(){
        LV_userOrder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(addPersionCard.this,Reservation.class);
                TextView TV_name = view.findViewById(R.id.TV_userNName);
                intent.putExtra("TV_Name",TV_name.getText().toString());
                startActivity(intent);
            }
        });
    }
    //返回界面刷新数据
    @Override
    protected void onResume() {
        super.onResume();
        initView();
    }
}